package com.ctshk.rpc.certificate.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ctshk.rpc.certificate.entity.CertificateBusiness;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;

/**
 * <p>
 * 证件业务 Mapper 接口
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-19
 */
public interface CertificateBusinessMapper extends BaseMapper<CertificateBusiness> {

    /**
     * 预约办理回乡证减次数
     * @param name
     * @param businessHallId
     * @param reserveDate
     * @param timeBucket
     * @return
     */
    @Update("UPDATE `t_certificate_business` main\n" +
            "LEFT JOIN t_certificate_business_booking_time time ON main.id = time.business_id\n" +
            "INNER JOIN t_certificate_business_booking_time_details detail ON time.id = detail.time_id \n" +
            "SET detail.remain_number = detail.remain_number - 1 \n" +
            "WHERE\n" +
            "\tmain.`status` = '1' \n" +
            "\tAND main.is_deleted = '0' \n" +
            "\tAND time.`status` = '1' \n" +
            "\tAND time.is_deleted = '0' \n" +
            "\tAND detail.`status` = '1' \n" +
            "\tAND detail.is_deleted = '0' \n" +
            "\tAND main.business_type_id = '2' \n" +
            "\tAND main.`name` = #{name} -- 业务名称\n" +
            "\tAND detail.business_hall_id = #{businessHallId} -- \t地点id\n" +
            "\tAND time.start_date <= #{reserveDate} AND time.end_date >= #{reserveDate} -- \t预约日期\n" +
            "\tAND detail.time_bucket = #{timeBucket} -- \t预约时间段\n" +
            "\tAND detail.remain_number - 1 >= 0")
    Long reserveReentryPermit(@Param("name")String name, @Param("businessHallId")Long businessHallId,
                              @Param("reserveDate")LocalDate reserveDate, @Param("timeBucket")String timeBucket);
}
